<?php
require "IProduct.php";
require "ProductDataCenter.php";
require "ProductLoadTemplate.php";
/**
 * 工厂类
 */
class ProductFactory
{
    const TYPE_BOOKS = 'Books';
    const TYPE_DOGS = 'Dogs';
    const TYPE_WINES = 'Wines';

    public static function getProduct($type)
    {
        if (is_string($type)){
            self::getProductByType($type);
        }
        if (is_array($type)){
            foreach ($type as $item){
                self::getProductByType($item);
            }
        }
    }

    /**
     * 通过类型获取产品对象
     * @param $type
     * @return Books|Dogs|false|Wines
     */
    public static function getProductByType($type){
        $obj = false;
        if (!class_exists($type)){
            require $type . '.php';
        }
        switch ($type){
            case self::TYPE_BOOKS:
                $obj = new Books();
                break;
            case self::TYPE_DOGS:
                $obj = new Dogs();
                break;
            case self::TYPE_WINES:
                $obj = new Wines();
                break;
        }
        if (is_subclass_of($obj, "IProduct")){
            ProductDataCenter::set($type, $obj);
        }else{
            return null;
        }
    }
}